// pages/owner/index.js
const app = getApp();
const { PageBase } = require('../../mixins/page');
const { getTabbarShowIndex } = require('../../biztools/main');
const { getLocationFailFlag, getUserInfo, setUserInfo } = require('../../biztools/info')
const { getMemberAccountInfo, userBindMobileWechat, userUpdate, checkMemberCard } = require('../../api/member');
const { getMemberCardDiscount, getCouponListCount } = require('../../api/market');
const { getExtInfo } = require('../../common/platform');

Page({
    mixins: [PageBase],
    data: {
        tabbar: {},
        owner: {},
        activeIndex: 0,
        couponTotal: 0,
        locationFail: getLocationFailFlag(),
        cardInfo: null,
        memberInfo: getUserInfo(),
        accountInfo: null,
        isHaveCard: false,
    },
    onShow() {
        const { tabbar, owner } = app.globalData.config;
        const activeIndex = getTabbarShowIndex(tabbar.list, 'owner');
        this.setData({
            activeIndex,
            tabbar,
            owner,
            memberInfo: getUserInfo(),
        }, async () => await this.getExtInfoAction());
    },
    async getExtInfoAction() {
        const { isLogin } = app.globalData;
        await this.checkGroupInfo();
        if (!isLogin) {
            setTimeout(async () => await this.getExtInfoAction(), 200);
            return;
        }
        await this._getCouponInfo()
        await this._getCardInfo();
    },
    async checkGroupInfo() {
        const { brand_id } = await getExtInfo();
        const isHaveCard = await checkMemberCard(brand_id);
        this.setData({
            isHaveCard,
        });
    },
    async _getCouponInfo() {
        const couponTotal = await getCouponListCount();
        this.setData({
            couponTotal,
        });
    },
    async _getCardInfo() {
        const accountInfo = await getMemberAccountInfo();
        console.log('Owner => 获取的会员信息为：', accountInfo);
        if (!accountInfo) return;
        // 获取卡折扣
        const discountInfo = await getMemberCardDiscount(accountInfo.card_id);
        console.log('Owner => 获取到的折扣信息', discountInfo);
        const cardInfo = {
            id: accountInfo.card_id,
            accId: accountInfo.acc_id,
            balance: accountInfo.balance,
            discount: discountInfo.discount / 10,
            cardName: accountInfo.card_name,
            integral: accountInfo.integral,
            integralUp: accountInfo.unit_integral_amount,
            rechargeUp: accountInfo.topup_limit,
            canRecharge: accountInfo.can_recharge,
            canIntegral: accountInfo.can_integral,
            cover: accountInfo.cover,
        };
        app.globalData.cardInfo = cardInfo;
        this.setData({
            cardInfo,
        });
    },
    // 更新会员信息
    async handleUpdateMember(e) {
        console.log('传递的参数为：', e.detail);
        const memberInfo = await userUpdate(e.detail);
        setUserInfo(memberInfo);
        this.setData({
            memberInfo,
        });
    },
    // 绑定手机号
    async handleBindMobile(e) {
        console.log(e);
        const { iv, encryptedData } = e.detail;
        if (!iv || !encryptedData) return;
        const memberInfo = await userBindMobileWechat(iv, encryptedData);
        setUserInfo(memberInfo);
        this.setData({
            memberInfo,
        });
    },
    // 联系商家
    contactAction() {
        console.log('Owner => 联系商家');
    },
})